var views_servlet ;
var viewedit_servlet ;

var commentMaxLength = 500;
var entityNameMaxLength = 50;
var viewNameNavMaxLength = 28;

$(document).ready(function(){
	views_servlet = WEBAPP+"views";
	viewedit_servlet = WEBAPP+"viewEdit";
	displayLang = $("#display-lang").val();//must be the name "displayLang" (for fetchValues)
	entityType = $("#etype").val();
	entityID = $("#eid").val();
	Genre = $("#genre").val();
	checkContentLengthWithMoreLess($(".comment-box"),commentMaxLength);
    checkContentLength($(".general-info-title .entity-label"),entityNameMaxLength);
    checkContentLength($(".entity-type span.type span"),55);
    /*start:customization buttons*/
    $(".custom_op").click(function(){
    	$("#smenu").hide();
    	hideGlobalSettingMenu();
    	
    	$("#emenu").toggle();
    });
    	
/*end:customization buttons*/
/*start:slide images*/
    $(".carousel").mouseover(function(){
    	if($(".next").length>0){
    		$(".next").show();
    		$(".prev").show();
    	}
    });
    
    $(".carousel").mouseout(function(){
    	if($(".next").length>0){
    		$(".next").hide();
    		$(".prev").hide();
    	}
    });
    
    $(".carousel").jCarouselLite({
    	btnNext: ".next",
    	btnPrev: ".prev",
    	visible: 1,		
    	speed: 500,	
    	scroll: 1	
    });
/*end:slide images*/
    
    /*navigation tabs*/
	$(".tab a").click(function(){
		 if($(this).parent().hasClass("selected")){
			 return;
		 }
		 
		 var oldSelected =  $(".tab.selected");
		 oldSelected.removeClass("selected");
		 $(this).parent().addClass("selected");
		 
		 storeOldViewCache(oldSelected);
		
		 var flag = readViewCache();
		 if(flag){
			 return;
		 }
		   var minTypeArray = getMinType();
			var isInner = minTypeArray[0] ;
			var minClassID = minTypeArray[1] ;
			var user = getUser();
		    var start = 0;
			var length =10;
			var lang =getDisplayLang();
			var curPage = 1;
			var userLogin = 0;
		    if($(this).hasClass("myview")){
		      if($("#groupID").length>0){
			   var link = $('<a href="javascript:void(0)" class="vcreate" title="Create a new view">New</a>');
			   if(displayLang=="zh"){
				   link.html("新建");
				   link.attr("title","创建一个新的视图");
			   }
			   if( $(".view-content").find(".vcreate").length==0){
				   $(".view-content .vnav-box .nv-box").append(link);
			   }
		      }
				 //process the popular entities
			    userLogin = 1;
				getViews("mine",isInner,minClassID,user, start,length,lang,curPage,userLogin);
			   }else if($(this).hasClass("popview")){
				   $(".view-content").find(".vcreate").remove();
				 //process mine entities
				   getViews("pop",isInner,minClassID,user, start,length,lang,curPage,userLogin);
		       }else if($(this).hasClass("trashedview")){
		    	   $(".view-content").find(".vcreate").remove();
		    		getViews("trash",isInner,minClassID,user, start,length,lang,curPage,userLogin);
		       }
	});
	
	init();

/*start:recommend entity*/
	getRecommends(Genre,entityID,displayLang);
	$(".recomdList li" ).live("mouseover",function(event){
       $(this).addClass("bh");
	});
	
	$(".recomdList li" ).live("mouseout",function(){
		$(this).removeClass("bh");
	});
	$(".recomdList li" ).live("click",function(){
		window.location = $(this).children("a").attr("loc");
	});
/*end:recommend entity*/

/*start:view set operation*/
	$(".vset" ).live("click",function(event){
        $('#smenu').toggle();
        event.stopPropagation();
	});
	
	$(".vset" ).live("mousedown",function(){
        $(this).addClass("ui-state-hover");
	});
	
	$(".vset" ).live("mouseup",function(){
        $(this).removeClass("ui-state-hover");
	});
/*end:view end operation*/
	
	/*bookmark*/
	$(".bmwarn_op").live("mouseover",function(){
		$(this).addClass("wbh");
	});
	
	$(".bmwarn_op").live("mouseout",function(){
		$(this).removeClass("wbh");
	});
	
	var buttonJSON;
	if(displayLang=="en")
		 buttonJSON = {
				"Done":function(){
				    if(nameCheck($("#bookmarkName"))){
				    	var bmname = $("#bookmarkName").val().trim();
				    	var flag = existBookmark(bmname,getUser(),displayLang,entityType,entityID);
				    	if(!flag){
				    		saveBookmark(bmname,getUser(),displayLang,entityType,entityID);
				    	}else{
				    		$("#warning-op").dialog({
				    			modal:true,
				    			width:350
				    		});
				    		
				    		$(".bmwarn_op.ov").unbind("click");
				    		$(".bmwarn_op.ov").bind("click",function(){
				    			saveBookmark(bmname,getUser(),displayLang,entityType,entityID);
				    			$("#warning-op").dialog("close");
				    		});
				    		
				    		$(".bmwarn_op.cancel").unbind("click");
				    		$(".bmwarn_op.cancel").bind("click",function(){
				    			$("#warning-op").dialog("close");
				    		});
				    	}
				    	$("#bookmark-op").dialog("close");
				    }
					return false;	
			   }
	    };
	else if(displayLang=="zh")
		buttonJSON = {
			"完成":function(){
				if(nameCheck($("#bookmarkName"))){
			    	var bmname = $("#bookmarkName").val().trim();
			    	var flag = existBookmark(bmname,getUser(),displayLang,entityType,entityID);
			    	if(!flag){
			    		saveBookmark(bmname,getUser(),displayLang,entityType,entityID);
			    	}else{
			    		$("#warning-op").dialog({
			    			modal:true,
			    			width:350
			    		});
			    		
			    		$(".bmwarn_op.ov").unbind("click");
			    		$(".bmwarn_op.ov").bind("click",function(){
			    			saveBookmark(bmname,getUser(),displayLang,entityType,entityID);
			    			$("#warning-op").dialog("close");
			    		});
			    		
			    		$(".bmwarn_op.cancel").unbind("click");
			    		$(".bmwarn_op.cancel").bind("click",function(){
			    			$("#warning-op").dialog("close");
			    		});
			    	}
			    	$("#bookmark-op").dialog("close");
			    }
				return false;	
		   }
        };
	$("a.bookmark_op").click(function(){
		$("textarea").val("");
		$("#bookmark-op").dialog({
			 modal:true,
			 width:350,
			 buttons:buttonJSON
		});
		return false;		
	});
	
	/*input hints*/
	$(".ftext").focus(function(){
		$(this).css("border-color","#256EB1");
		$(this).parent().find(".errorMsg").hide();
	});
	
	/*comment*/
	$("a.more-content").live("click",function(){
		var html = $(this).parent().find(".hidden").html();
		var parent = $(this).parent();
		parent.html(html);
		var less = $("<a class='less-content'>Less</a>");
		$(parent).append(less);
	});
	
	$("a.less-content").live("click",function(){
		var parent=$(this).parent();
		$(this).parent().find("a.less-content").remove();
		checkContentLengthWithMoreLess(parent,commentMaxLength);
	});
	
	$("a.entity_edit").click(function(){
		 var url = window.location.href;
  		 var location = WEBAPP+"views/identity.jsp?genre="+Genre+"&eid="+entityID;
  		 var name = $(".entity-label").html();
  		 name = encodeURIComponent(encodeURIComponent(name));
  		 location = location +"&n="+name;
  		 if(displayLang=="zh"){
  		    location =  location+"&lang=zh" ;
  		 }
  		 window.location = location;
	});
	
	$("a.entity_new").click(function(){
		 var url = window.location.href;
 		 var location = WEBAPP+"views/identity.jsp?genre="+Genre+"&eid="+entityID;
 		 var name = $(".entity-label").html();
 		 name = encodeURIComponent(encodeURIComponent(name));
 		 location = location +"&n="+name;
 		 if(displayLang=="zh"){
 		    location =  location+"&lang=zh" ;
 		 }
 		 window.location = location;
	});
	
	/*left nav*/
	   $(".dmenu li").live("mouseover",function(){
		   if(!$(this).hasClass("mspacingLittle")){
			   $(this).addClass("ah");
		   }
	   });
	   $(".dmenu li").live("mouseout",function(){
		   $(this).removeClass("ah");
	   });
	   
	   
	   $(".view-list li").live("mouseover",function(){
		   $(this).addClass("bh");
		   $(this).find(".viewOp").show();
	   });
	   $(".view-list li").live("mouseout",function(){
		   $(this).removeClass("bh");
		   $(this).find(".viewOp").hide();
	   }); 
	   $(".view-list li").live("click",function(){
		   if($(".view-list").find(".nav-select").attr("vid")==$(this).attr("vid")){
			   return;
		   }
           $(".view-content .view-list").find(".nav-select").removeClass("nav-select");
           $(this).parent().find('.nav-arrow').remove();
           $(this).addClass("nav-select");
           var span = $("<span class='nav-arrow'></span>");
           $(this).append(span);
           
           var vid = parseInt($(this).attr("vid"));
           var vname = $(this).find(".viewName").text();
         
           showView(vid,vname,$(".view-content .vcontent"));
	   });
	   /**cancel favorite**/
	   $("a.down-op").live("click",function(event){
		   var li = $(this).parent().parent();
		   li.remove();
		   li.removeClass("bh");
		   if(li.hasClass("nav-select")){
			   li.removeClass("nav-select");
			   li.find(".nav-arrow").remove();
			   $(".view-content .vcontent").html("");
		   }
		   li.find(".down-op").remove();
	       var favorOp = $('<a href="javascript:void(0)" class="top-op"></a>')
	    	.append($("<span class='top'></span>"));
	    	if(displayLang=='en'){
	    		favorOp.attr("title","Favourite");
	    	}else if(displayLang=='zh')
	    	    favorOp.attr("title","最爱");
	       li.find(".viewOp").append(favorOp) ;
	       li.find(".viewOp").hide();
		   li.insertBefore($(".av .vnav ul li").eq(-2));
		   cancelFavorite(li);
		   event.stopPropagation();
	   });
	   
	   /*Favourite and update the database. favor and cancel favor*/
	   $("a.top-op").live("click",function(event){
	       var li = $(this).parent().parent();
	       li.remove();
	       li.removeClass("bh");
		   if(li.hasClass("nav-select")){
			   li.removeClass("nav-select");
			   li.find(".nav-arrow").remove();
			   $(".view-content .vcontent").html("");
		   }
	       li.find(".top-op").remove();
	       var cancelOp = $('<a href="javascript:void(0)" class="down-op"></a>')
	    	.append($("<span class='down'></span>"));
	    	if(displayLang=='en'){
	    		cancelOp.attr("title","Cancel Favourite");
	    	}else if(displayLang=='zh')
	    		cancelOp.attr("title","取消最爱");
	    	li.find(".viewOp").append(cancelOp);
	    	li.find(".viewOp").hide();
		   li.appendTo(".fv .vnav ul");
		   addFavorite(li);
		   event.stopPropagation();
	   });
	   
	   /*Remove into trash and update the database. trash and cancel trash*/
	   $("a.trash-op").live("click",function(event){
		   var li = $(this).parent().parent();
		   li.remove();
		   li.removeClass("bh");
		   if(li.hasClass("nav-select")){
			   li.removeClass("nav-select");
			   li.find(".nav-arrow").remove();
			   $(".view-content .vcontent").html("");
		   }
		   var flag = $(this).parent().parent().parent().parent().hasClass("myview");
		   li.prependTo(".trashedview ul");
		   if(flag){
			   li.find(".down-op").remove();
			   cancelFavorite(li) ;
		   }else{
			   li.find(".top-op").remove(); 
		   }
		   li.find(".trash-op").remove();
	    	var restoreOp = $('<a href="javascript:void(0)" class="cancel-op"></a>')
	    	.append($("<span class='restore'></span>"));
	    	if(displayLang=='en'){
	    		restoreOp.attr("title","Restore");
	    	}else if(displayLang=='zh')
	    		restoreOp.attr("title","还原");
	      li.find(".viewOp").append(restoreOp);
	    	li.find(".viewOp").hide();
	    	li.appendTo(".tv .vnav ul");
		   cancelFavorite(li);
		   addTrash(li);
		   event.stopPropagation();
	   });	 
	   
	   $("a.cancel-op").live("click",function(event){
		   var li = $(this).parent().parent();
		   li.remove();
		   li.removeClass("bh");
		   if(li.hasClass("nav-select")){
			   li.removeClass("nav-select");
			   li.find(".nav-arrow").remove();
			   $(".view-content .vcontent").html("");
		   }
		   li.find(".cancel-op").remove();
	       var favorOp = $('<a href="javascript:void(0)" class="top-op"></a>')
	    	.append($("<span class='top'></span>"));
	    	if(displayLang=='en'){
	    		favorOp.attr("title","Favourite");
	    	}else if(displayLang=='zh')
	    	    favorOp.attr("title","最爱");		   
		   li.find(".viewOp").append(favorOp);
	       var rmOp = $('<a href="javascript:void(0)" class="trash-op"></a>')
	    	.append($("<span class='remove'></span>"));
	    	if(displayLang=='en'){
	    		rmOp.attr("title","Remove");
	    	}else if(displayLang=='zh')
	    		rmOp.attr("title","移除");
	    	li.find(".viewOp").append(rmOp);   
	    	li.find(".viewOp").hide();
	    	li.insertBefore($(".av .vnav ul li").eq(-2));
		   cancelTrash(li);
		   event.stopPropagation();
	   });
	   
	   /* right panel*/
	   $(".pvalue li").live("mouseover",function(){
		   $(this).addClass("bh");
	   });
	   $(".pvalue li").live("mouseout",function(){
		   $(this).removeClass("bh");
	   });	   
	   /**titlebar**/
	   $(".titlebar").live("click",function(){
		   $(this).find(".status").toggleClass("collapse");
		   $(this).parent().find(".content").toggle('fast');
	   });
	   
	   /***explore***/
	   $("span.explore").live("mouseover",function(){
		   $(this).addClass("hover");
	   });
	   
	   $("span.explore").live("mouseout",function(){
		   $(this).removeClass("hover");
	   });
	   
	   $("span.explore").live("click",function(event){
		   event.stopPropagation(); 
		   var collection = new Array();
		   $(this).parent().parent().find(".content .pvalue li").each(function(){
			   var etype = $(this).attr("etype");
			   var eid = $(this).attr("eid");
			   var object = {"etype":etype,"eid":eid};
			   collection.push(object);
		   });
		   var entityLabel = $('.entity-label').html();
		   var dmtitle= $(this).parent().find(".title").html();
		   var name = entityLabel+"."+dmtitle;
		   browseCollection(collection,name);
		   
	   });
	   
	   /*start of view set operations*/ 
	   
	   $(".vcreate").click(function(){
		   var url = window.location.href;
		   if($("#groupID").length==0){
		   var edit_authorDiv = createGroupSelectionDiv(getUser(),displayLang);
		   $(edit_authorDiv).dialog({
	    		 modal:true,
				 minWidth:450,
		         height:200,
		         buttons:{
		        	"OK":function(){
		        		var type = $(".edit_author").attr("t") ;
		        		if(type=="y"){
		        			setGroup();
		        			var genre = $(".entity-label").attr("genre");
			    			var eid = parseInt($(".entity-label").attr("eid"));
			    			var minType = getMinType();
			    			var isInner = minType[0];
			    			var cid = minType[1];
			    			var location = WEBAPP+"views/evedit.jsp?genre="+genre+"&eid="+eid+"&i="+isInner+"&cid="+cid+"&lang="+displayLang;
			    			window.location=location;
		        		}else if(type=="n"){
			    			var paraString = encodeURIComponent(url);
			    			var location = WEBAPP+"group/gmanage.jsp?from="+paraString;
			    			if(displayLang=="zh")
			    				location = WEBAPP+"group/gmanage.jsp?lang=zh&from="+paraString;
			    			window.location= location;
		        		}
		    			
				     },
				    "Cancel":function(){
					 $(".edit_author").dialog("close");
				    }
		         }
	       });
		   }else{
   				var genre = $(".entity-label").attr("genre");
   				var eid = parseInt($(".entity-label").attr("eid"));
   				var minType = getMinType();
   				var isInner = minType[0];
   				var cid = minType[1];
   				var location = WEBAPP+"views/evedit.jsp?genre="+genre+"&eid="+eid+"&i="+isInner+"&cid="+cid+"&lang="+displayLang;
   				window.location=location;
		   }
	   });
	   
	   $(".view-edit").live("click",function(){
			var genre = $(".entity-label").attr("genre");
			var eid = parseInt($(".entity-label").attr("eid"));
			var vid = $(this).parent().parent().parent().parent().parent().attr("vvid");
			var minType = getMinType();
			var isInner = minType[0];
			var cid = minType[1];
			var location = WEBAPP+"views/evedit.jsp?genre="+genre+"&eid="+eid+"&vid="+vid+"&i="+isInner+"&cid="+cid+"&lang="+displayLang;
			window.location= location;
	   });
	   
	   //set view layout for user
	   $(".view-layout").live("click",function(){
		   //show the setting panel
		   var settingOp = getLayoutSettingDiv();
		   $(".view-content .vcontent").find(".vname").after(settingOp);
	   });
	   
       $(".layoutOp .setting-save .sbutton").live("click",function(){
		 var value = $("input[name=layout]:checked").val();
		 
		  //change the display column of the view
		 if(value==1){
			$(".view-content .vcontent .prop").removeClass("two-column");
			$(".view-content .vcontent .prop").removeClass("three-column");
		 }else if(value==2){
			$(".view-content .vcontent .prop").removeClass("three-column");
			if(!$(".view-content .vcontent .prop").hasClass("two-column"))
				$(".view-content .vcontent .prop").addClass("two-column");
		 }else if(value==3){
			$(".view-content .vcontent .prop").removeClass("two-column");
			if(!$(".view-content .vcontent .prop").hasClass("three-column"))
				$(".view-content .vcontent .prop").addClass("three-column");
		 }
		 
		 //store the layout for user in  the databases
		 var vid = $(".view-content .vcontent .relative").attr("vvid");
		 if(vid>0)
			 storeLayout(getUser(),vid,value);
		 $(this).parent().parent().parent().remove();
       });
	   
       $(".layoutOp .setting-save .cbutton").live("click",function(){
    	   $(this).parent().parent().parent().remove();
       });
       
	   $(".view-translate").live("click",function(){
		    var genre = $(".entity-label").attr("genre");
			var eid = parseInt($(".entity-label").attr("eid"));
			var vid = $(this).parent().parent().parent().parent().parent().attr("vvid");
			var location = WEBAPP+"views/translate.jsp?genre="+genre+"&eid="+eid+"&vid="+vid+"&lang="+displayLang;
			window.location= location;
	   });
	   
	   $(".transview-edit").live("click",function(){
		    var genre = $(".entity-label").attr("genre");
			var eid = parseInt($(".entity-label").attr("eid"));
			var vid = $(this).attr("vid");
			var location = WEBAPP+"views/translate.jsp?genre="+genre+"&eid="+eid+"&vid="+vid+"&lang="+displayLang;
			window.location= location;
	   });
	   
	   /*end of view set operations*/ 
});
function getRecommends(type_str, id, lang){
//	alert(type_int+"|"+id+"|"+lang);
	var data = {};
	data["type_str"]=type_str+"";
	data["id"]=id+"";
	data["lang"]=lang;
	data = JSON.stringify(data);
//	alert(data+"|"+WEBAPP+"recommendServlet");
	data = ("getRecommends="+encodeURIComponent(data));
	$.ajax({
		url: WEBAPP+"recommendServlet",
		data: data,
		async: true,
		dataType: 'text',//for chinese messy code //'json',
		type: 'post',
		beforeSend:function(){
			var container = $(".recommend-box");
			$(container).html("");
			$(container).addClass("loading2");
		},
		success: function(result_str){
			var result_text = decodeURIComponent(result_str);
			var result = eval("("+result_text+")");
			var container = $(".recommend-box");
			$(container).removeClass("loading2");
			var lang = $("#display-lang").val();
			if(result==null || result.status!="ok"){
				return;
			}else{
//				$(container).html("");
				var relArray = result["byRelated"];
				if(relArray != null && relArray.length>0){
					var title = "Related entities";
					if(lang=="zh"){
						title = "相关实体";
					}
					buildRecommendBox(container, relArray, title);
				}//end of if_relArray
				
				var histArray = result["byHistory"];
				if(histArray != null && histArray.length>0){
					var title = "Users who visited it also visited";
					if(lang=="zh"){
						title = "访问过此实体的用户还访问过";
					}
					buildRecommendBox(container, histArray, title);
				}//end of if_hisArray
				
			}
			
		}
	});
}
function buildRecommendBox(container, array, title){
	if(array == null || $(container).length<=0){
		return;
	}
	var relBox = $("<div class='log-rcmdbox'></div>");
	var titleDiv = $("<div class='rcmd-title'></div>");
	$(titleDiv).text(title);
	$(relBox).append(titleDiv);
	var contentBox = $("<div class='contentBox'></div>");
	$(relBox).append(contentBox);
	var listUl = $("<ul class='recomdList'></ul>");
	$(relBox).append(listUl);
	$(container).append(relBox);
	for(var i=0; i<array.length; i++){
		var item = array[i];
		var id = item["id"];
		var genre = item["genre"];
		var name = item["name"];
		var li = $("<li></li>");
		var a = $("<a></a>");
		$(a).text(name);

		if($(listUl).find("li[eid="+eid+"]").length>0){
			if($(listUl).find("li[eid="+eid+"]").find("a").html().trim()==name){
				   continue;
			}
		 }
		
		var href =WEBAPP+"entity/";
		var eid;
		if(genre==COLLECTION_NO){
			href=href+COLLECTION_STR;
			eid=COLLECTION_STR ;
		}else if(genre==CUSTOM_ENTITY_NO){
			href = href+CUSTOM_ENTITY_STR;
			eid=CUSTOM_ENTITY_STR ;
		}else if(genre==URI_ENTITY_NO){
			href= href+URI_ENTITY_STR ;
			eid=URI_ENTITY_STR ;
		}
		href =href+id;
		eid = eid + id;

		$(li).attr("eid",eid);
		$(listUl).append(li);
		
		if(displayLang=="zh"){
			href = href+"?lang=zh" ;
		}
		a.attr("loc",href) ;
		$(li).append(a);
	}
}

function addFavorite(e){
	var minTypeArray = getMinType();
	var isInner = minTypeArray[0] ;
	var classID = minTypeArray[1] ;
	var lang =getDisplayLang();
	var user = getUser();
	var vid = $(e).attr("vid");
	 var data={};
	 data["user"] = user;
	 data["viewID"] = vid;
	 data["isInner"] = isInner;
	 data["classID"] = classID;
	 data["lang"] = lang;
     data =  JSON.stringify(data);
    data = ("addFavorite="+encodeURIComponent(data));
    $.ajax({
  	  url:views_servlet,
  	  async:true,
  	  data:data,
  	  dataType:"json",
  	  type:"post" 
    });
}

function addTrash(e){
	var minTypeArray = getMinType();
	var isInner = minTypeArray[0] ;
	var classID = minTypeArray[1] ;
	var lang =getDisplayLang();
	var user = getUser();
	var vid = $(e).attr("vid");
	 var data={};
	 data["user"] = user;
	 data["viewID"] = vid;
	 data["isInner"] = isInner;
	 data["classID"] = classID;
	 data["lang"] = lang;
     data =  JSON.stringify(data);
    data = ("addTrash="+encodeURIComponent(data));
    $.ajax({
  	  url:views_servlet,
  	  async:true,
  	  data:data,
  	  dataType:"json",
  	  type:"post" 
    });
}

function blurState(e){
	$(e).css("border-color","#BDC7D8");
}

function browseCollection(collection,name){
	 var data={};
	 data["lang"] = displayLang;
	 data["collection"] = collection;
	 data["name"] = name;
     data =  JSON.stringify(data);
     data =("tmpcollection="+encodeURIComponent(data));
     $.ajax({
  	   url:"/sview/collection",
  	   async:true,
  	   data:data,
  	   dataType:"text",
  	   type:"post" ,
  	   success:function(collectionID){
  		  var location = "/sview/entity/c"+collectionID;
  		  location = location+"?lang="+displayLang;
  		  window.location = location;
  	   }
     });
}

function cancelFavorite(e){
	var minTypeArray = getMinType();
	var isInner = minTypeArray[0] ;
	var classID = minTypeArray[1] ;
	var user = getUser();
	var vid = $(e).attr("vid");
	 var data={};
	 data["user"] = user;
	 data["viewID"] = vid;
	 data["classID"] = classID;
	 data["isInner"] = isInner;
     data =  JSON.stringify(data);
    data =("cancelFavorite="+encodeURIComponent(data));
    $.ajax({
  	  url:views_servlet,
  	  async:true,
  	  data:data,
  	  dataType:"json",
  	  type:"post" 
    });
}


function cancelTrash(e){
	var minTypeArray = getMinType();
	var isInner = minTypeArray[0] ;
	var classID = minTypeArray[1] ;
	var user = getUser();
	var vid = $(e).attr("vid");
	 var data={};
	 data["user"] = user;
	 data["viewID"] = vid;
	 data["isInner"]= isInner;
	 data["classID"] = classID;
     data =  JSON.stringify(data);
    data =("cancelTrash="+encodeURIComponent(data));
    $.ajax({
  	  url:views_servlet,
  	  async:true,
  	  data:data,
  	  dataType:"json",
  	  type:"post" 
    });
}

function checkAuthority(vid){
	 var array = new Array() ;
	 var data={};
	 data["viewID"] = vid;
	 var user = getUser();
	 data["user"] = user;
     data =  JSON.stringify(data);
     data =("checkAuthor="+encodeURIComponent(data));
     $.ajax({
 	  url:views_servlet,
 	  async:false,
 	  data:data,
 	  dataType:"json",
 	  type:"post",
      success:function(res){  
    	  var hasAuthor = res["hasAuthor"];
    	  var gid = res["gid"];
    	  var isTrans = res["isTranslatedView"];
    	  if(hasAuthor=="yes"){
    		  array.push(true);
    	  }else
    		  array.push(false);
    	  array.push(gid);
    	  array.push(isTrans);
    	  if(isTrans==true){
    		  array.push(res["srcvid"]);
    	  }
      }
     });
    
     return array ;
}

function getDisplayLang(){
	var displayLang = $("#display-lang").val();
	return displayLang;
}

function getLayoutSettingDiv(){
	var layoutDiv  = $(' <div class="layoutOp"></div>');
	var mspacingDiv = $('<div class="mspacingSetting"></div>');
	layoutDiv.append(mspacingDiv.clone());
	var label = $('<label class="settingTitle"></label>');
	if(displayLang=="en")
		label.html("Layout");
	else if(displayLang=="zh")
		label.html("布局");
	layoutDiv.append(label);
	var settingContent = $(' <div class="settingContent"></div>');
	layoutDiv.append(settingContent);
	layoutDiv.append(mspacingDiv.clone());
    for(i=1;i<=3;i++){
    	var settingItem = $('<div class="setting-item"></div>');
    	settingContent.append(settingItem);
    	var spanColumnNum = $(' <span class="columnNum"></span>');
    	if(i==1){
    		if(displayLang=="en")
    			spanColumnNum.html(i+" column");
    		else if(displayLang=="zh")
    			spanColumnNum.html(i+" 列");
    	}
    	else {
    		if(displayLang=="en")
    			spanColumnNum.html(i+" columns");
    		else if(displayLang=="zh")
    			spanColumnNum.html(i+" 列");
    	}
    	settingItem.append(spanColumnNum);
    	var  span = $("<span></span>");
    	settingItem.append(span);
    	var raido ;
    	if(i==1)
    		radio = $(' <input type="radio" checked name="layout">');
    	else
    		radio = $(' <input type="radio"  name="layout">');
    	radio.val(i);
    	span.append(radio);
    	var img;
    	if(i==1){
    		img = $('<img src="/sview/icons/layout1.png"></img>');
    	}else if(i==2){
    		img = $('<img src="/sview/icons/layout3.png"></img>');
    	}else if(i==3){
    		img = $('<img src="/sview/icons/layout5.png"></img>');
    	}
    	span.append(img);
    }
    settingContent.append(mspacingDiv.clone());
    var  save ;
    if(displayLang=="en"){
    	save = $('<div class="setting-save"> <input value="Save" class="sbutton fb" type="button"><input value="Cancel" class="cbutton fb" type="button"></div>');
    }else if(displayLang=="zh"){
    	save = $('<div class="setting-save"> <input value="保存" class="sbutton fb" type="button"><input value="取消" class="cbutton fb" type="button"></div>');
    }
    
    
    settingContent.append(save);
    return layoutDiv;
}

function getTypes(){
	var array =new Array();
	$(".entity-type .type").each(function(){
		 $(this).find("span").each(function(){
			 var tid = $(this).attr("type_id");
			 array.push(tid); 
		 });
	});
	return array;
}

/**
 * get only min class id of the view which may be innerclass
 * @returns {Array} first:isInner,second:classID
 */
function getMinType(){
	var array = new Array();
    if($("#minTypes").length>0){
    	var ele = $("#minTypes");
    	array.push(ele.attr("inner"));
    	array.push(ele.attr("cid"));
    }
	return array;
}

function getTypeSelected(){
	var tid = $(".ts option:selected").attr("tid");
	return tid;
}

function isInnerClass(){
	var ttype = $(".ts option:selected").attr("ttype");
	if(ttype=="i")
		return true;
	return false;
}

function getViewDatamember(vid,ul,etype,eid){
	 var data={};
	 data["viewID"] = vid;
	 data["lang"] = displayLang;
	 data["etype"] = etype;
	 data["eid"] = eid;
	 var user = getUser();
	 data["user"] = user;
    data =  JSON.stringify(data);
    data =("viewDMList="+encodeURIComponent(data));
   $.ajax({
 	  url:views_servlet,
 	  async:true,
 	  data:data,
 	  dataType:"json",
 	  type:"post",
 	  beforeSend:function(){
		  $(ul).addClass("loading2");
	  },
     success:function(res){
   	  $(ul).removeClass("loading2");
   	  
         var column = res["column"];    	  
   	  var result = res["list"];
   	  
   	  var dmids = new Array();
   	  var dmtypes =new Array();
   	  var inverses = new Array();
   	  
   	  for(i=0;i<result.length;i++){
   		  var item=result[i];
   		  var dmtype = item["dmtype"];
   		  var dmid = item["dmid"];
   		  dmids.push(dmid);
   		  dmtypes.push(dmtype);
   		  var name = item["name"];
   		  var inverse  = item["inverse"] ;
   		  inverses.push(inverse);
   		  var li = $('<li class="prop"></li>');
   		  ul.append(li);
   		  li.attr("dmid",dmid);
   		  li.attr("dmtype",dmtype);
   		  li.attr("inverse",inverse);
   		  
   		  /*set display column*/
   		  if(column==2){
   			  li.addClass("two-column");
   		  }else if(column==3){
   			  li.addClass("three-column");
   		  }
   			  
   		  var titlebar = $("<div class='titlebar'></div>");
   		  li.append(titlebar);
   		  var spanLoading = $("<span class='loading'></span>");
   		  titlebar.append(spanLoading);
   		  var spanTitle = $("<span class='title'></span>");
   		  spanTitle.text(name);
   		  var status = $('<a class="status"></a>');
   		  titlebar.append(status);
   		  titlebar.append(spanTitle);
   		  var explorerSpan = $("<span class='explore'></span>");
   		  if(displayLang=="en"){
   			explorerSpan.attr("title","Explore this collection");
   		  }else if(displayLang=="zh"){
   			explorerSpan.attr("title","浏览取值集合") ; 
   		  }
   		  titlebar.append(explorerSpan);
   		  var content = $("<div class='content'></div>");
   		  var pvalue = $("<ul class='pvalue'></ul>");
   		  pvalue.attr("dmtype",dmtype);
   		  pvalue.attr("dmid",dmid);
   		  li.append(content) ;
   		  content.append(pvalue);
   	  }
   	  
   	  /*must be out of the above loop*/
   	  for(i=0;i<dmids.length;i++){
   		  var dmid = dmids[i];
   		  var dmtype = dmtypes[i];
   		  var inverse = inverses[i];
   		  var container = ul.find("li[dmid="+dmid+"][dmtype="+dmtype+"]").find(".pvalue");
   		  var genre = $(".entity-label").attr("genre");
   		  var eid = parseInt($(".entity-label").attr("eid"));
   		  var status = "undownloaded" ;
   		  fetchValuesInBrowsing(displayLang,container,dmtype,dmid,inverse,genre,eid,status);
   	  }
   	  
     }
   });
}

function getViews(type,isInner, minClassID,user,start,length,lang,currentPage,userLogin){
	 var data={};
	 data["start"] = start;
	 data["length"] = length;
	 data["lang"] = lang;
	 data["user"] = user;
	 data["isInner"] = isInner;
	 data["classID"] = minClassID;
    data =  JSON.stringify(data);
    if(type=="pop")
   	 data=("pop="+encodeURIComponent(data));
    else if(type=="mine")
   	 data =("mine="+encodeURIComponent(data));
    else if(type=="trash")
   	 data =("trash="+encodeURIComponent(data));
	 $.ajax({
	  url:views_servlet,
	  async:true,
	  data:data,
	  dataType:"json",
	  type:"post" ,
	  success:function(result){
		var container = $(".view-content .vnav");
		container.html("");
		var ul = $("<ul class='view-list'></ul>");
		container.append(ul);
		var totalNum = result["totalNum"];
		var totalPage = Math.ceil(totalNum/length);
		var views = result["views"];
		for(i=0;i<views.length;i++){
			var view = views[i];
		    var name = view["name"];
		    var id = view["id"];
		    var li = $("<li></li>");
		    li.addClass("uView");
		    li.attr("vid",id);
		    ul.append(li);
		    var spanTitle = $("<span class='viewName'></span>");
		    li.append(spanTitle);
		    name= cutText(name,viewNameNavMaxLength);
		    spanTitle.text(name) ;
		    /*operations*/
		    var spanOp = $("<span class='viewOp'></span>") ;
		    li.append(spanOp);
		    //pop views
		    var user = getUser();
		    if(user!=null){
		    if(type=="pop"){
		    	var favorOp = $('<a href="javascript:void(0)" class="top-op"></a>')
		    	.append($("<span class='top'></span>"));
		    	if(displayLang=='en'){
		    		favorOp.attr("title","Favourite");
		    	}else if(displayLang=='zh')
		    	    favorOp.attr("title","最爱");
		    	spanOp.append(favorOp);
		    	
		    	var rmOp = $('<a href="javascript:void(0)" class="trash-op"></a>')
		    	.append($("<span class='remove'></span>"));
		    	if(displayLang=='en'){
		    		rmOp.attr("title","Remove");
		    	}else if(displayLang=='zh')
		    		rmOp.attr("title","移除");
		    	spanOp.append(rmOp);
		    }else if(type=="mine"){
		    	var cancelOp = $('<a href="javascript:void(0)" class="down-op"></a>')
		    	.append($("<span class='down'></span>"));
		    	if(displayLang=='en'){
		    		cancelOp.attr("title","Cancel Favourite");
		    	}else if(displayLang=='zh')
		    		cancelOp.attr("title","取消最爱");
		    	spanOp.append(cancelOp);
		    	
		    	var rmOp = $('<a href="javascript:void(0)" class="trash-op"></a>')
		    	.append($("<span class='remove'></span>"));
		    	if(displayLang=='en'){
		    		rmOp.attr("title","Remove");
		    	}else if(displayLang=='zh')
		    		rmOp.attr("title","移除");
		    	spanOp.append(rmOp) ;
		    }else if(type=="trash"){
		    	var cancelOp = $('<a href="javascript:void(0)" class="cancel-op"></a>')
		    	.append($("<span class='restore'></span>"));
		    	if(displayLang=='en'){
		    		cancelOp.attr("title","Restore");
		    	}else if(displayLang=='zh')
		    		cancelOp.attr("title","还原");
		    	spanOp.append(cancelOp);
		    }
		    }
		}
		//for pop views add the forward and backward
		if(type=="pop"){
		    var liForward = $("<li></li>");
		    liForward.attr("vid",-1);
		    liForward.addClass("sView");
		    ul.append(liForward);
		    var spanTitleForward = $("<span class='viewName'></span>");
		    liForward.append(spanTitleForward);
		    if(displayLang=="en")
		    	spanTitleForward.text("Forward");
		    else if(displayLang=="zh")
		    	spanTitleForward.text("前向属性");
		    /*operations*/
		    var spanOpForward = $("<span class='viewOp'></span>") ;
		    liForward.append(spanOpForward);
		    
		    var liBackward = $("<li></li>");
		    liBackward.attr("vid",-2);
		    liBackward.addClass("sView");
		    ul.append(liBackward);
		    var spanTitleBackward = $("<span class='viewName'></span>");
		    liBackward.append(spanTitleBackward);
		    if(displayLang=="en")
		    	spanTitleBackward.text("Backward");
		    else if(displayLang=="zh")
		    	spanTitleBackward.text("后向属性");
		    /*operations*/
		    var spanOpBackward = $("<span class='viewOp'></span>") ;
		    liForward.append(spanOpBackward);
		    
		}
		
		//paging information
		var navDiv = $("<div class='navfoot'></div>");
		container.append(navDiv);
		if(currentPage>1){
			var prevaLink = $('<a  class="navContent pnnav" href="javascript:void(0)"></a>');
			prevaLink.bind("click",function(){
				var mstart = (start-length)<0?0:(start-length);
				getViews(type,isInner, minClassID,user,mstart,length,lang,currentPage-1);
			});
			if(displayLang=='en')
				prevaLink.text("Prev");
			else if(displayLang=="zh")
				prevaLink.text("上一页");
			navDiv.append(prevaLink);
		}
		
		/*in the view paging, 5 pages shown*/
		var startPage = (currentPage-2)>0?(currentPage-2):1;
		var endPage = (currentPage+2)>totalPage?(totalPage>0?totalPage:1):(currentPage+2);
		if(endPage>startPage){
		for(var j=startPage;j<=endPage;j++){
			if(j!=currentPage){
				var alink =$('<a class="navContent" href="javascript:void(0)"></a>');
				alink.text(j);
				alink.click(function(){
					var pointer = parseInt($(this).text());
					getViews(type, isInner,minClassID,user,(pointer-1)*length,length,lang,pointer);
				});
				navDiv.append(alink);
			}else{
				var span = $("<span class='navContent current' ></span>");
				span.text(j);
				navDiv.append(span);
			}
		}
		}
		if(currentPage<totalPage){
			var nextaLink = $('<a  class="navContent pnnav" href="javascript:void(0)"></a>');
			nextaLink.bind("click",function(){
				getViews(type, isInner,minClassID,user,start+length,length,lang,currentPage+1);
			});
			if(displayLang=='en')
				nextaLink.text("Next");
			else if(displayLang=="zh")
				nextaLink.text("下一页");
			navDiv.append(nextaLink);
		}	
		
	    if(userLogin==1){ 
	       if(type=="mine"){
	    	   $(".view-content .view-list").find(".nav-select").removeClass("nav-select");
	    	   $('.view-content .nav-arrow').remove();
		       var target = ul.find("li:first");
		       if(target.length>0){
		    	   target.addClass("nav-select");
		    	   var span = $("<span class='nav-arrow'></span>");
		    	   target.append(span);
		    	   
		    	   var vid = parseInt(target.attr("vid"));
		    	   var vname =target.find(".viewName").text();
		    	   showView(vid,vname,$(".view-content .vcontent"));
		       }
	    	}
	    }else if(userLogin==0){
	    	  $(".view-content .view-list").find(".nav-select").removeClass("nav-select");
	    	  $('.view-content .nav-arrow').remove();
	    	   var target = ul.find("li:first");
		       if(target.length>0){
		    	   target.addClass("nav-select");
		    	   var span = $("<span class='nav-arrow'></span>");
		    	   target.append(span);
		    	   
		    	   var vid = parseInt(target.attr("vid"));
		    	   var vname =target.find(".viewName").text();
		    	   showView(vid,vname,$(".view-content .vcontent"));
		       }
	    }
	  }
	});	
}

function getViewType(e){
	var type ;
	if($(e).hasClass("myview"))
		type="mine";
	else if($(e).hasClass("trashedview"))
		type ="trash";
	else
		type = 	"pop";
	return type;
}

function init(){
	//get the pop views 
	var minTypeArray = getMinType();
	var isInner = minTypeArray[0] ;
	var minClassID = minTypeArray[1] ;
	var user = getUser();
    var start = 0;
	var length =10;
	var lang =getDisplayLang();
	var curPage = 1;
	var userLogin = 0;;
	if($(".myview").length>0){
		if($("#groupID").length>0){
			 var link = $('<a href="javascript:void(0)" class="vcreate" title="Create a new view">New</a>');
			 $(".view-content .vnav-box .nv-box").append(link);
			 if(displayLang=="zh"){
			   link.html("新建");
			   link.attr("title","创建一个新的视图");
			 }
		 }
		userLogin = 1;
	    $(".myview").parent().addClass("selected");
		getViews("mine",isInner,minClassID,user, start,length,lang,curPage,userLogin);
	}else{
		 $(".popview").parent().addClass("selected");
		getViews("pop",isInner,minClassID,user, start,length,lang,curPage,userLogin);
	}
	
	$(".entity-type .type>span").each(function(){
		if($(this).text()==""){
			fetchLabel($(this),parseInt($(this).attr("type_id")),displayLang);
		}
	});
}

function nameCheck(e){
	var displayLang = $("#display-lang").val();
	var flag =true;
	blurState(e);
	var val =$(e).val();
	if(val==""){
		var msg =$(e).parent().find(".errorMsg");
		var textEn = "Please enter a name";
		var textZhSimple ="请输入名称";
		if(displayLang=="en")
			msg.text(textEn);
		else if(displayLang=="zh")
			msg.text(textZhSimple);
		msg.show();
		$(e).css("border-color","red");
		flag = false;
	}else if(val.indexOf("%")>=0){
		var msg =$(e).parent().find(".errorMsg");
		var textEn = "Illegal character '%'";
		var textZhSimple ="非法字符'%'";
		if(displayLang=="en")
			msg.text(textEn);
		else if(displayLang=="zh")
			msg.text(textZhSimple);
		msg.show();
		$(e).css("border-color","red");
		flag = false;
	}
	if(!flag)
		return false;
	return true;
}

function existBookmark(name,author,lang,etype,eid){
	 var flag = false;
	 var data={};
	 data["author"] = author;
	 data["name"] = name;
	 data["lang"] = lang;
	 data["etype"] = etype;
	 data["eid"] = eid;
	 data =  JSON.stringify(data);
	 data =("existBookmark="+encodeURIComponent(data));
	 $.ajax({
	   	  url:views_servlet,
	   	  async:false,
	   	  data:data,
	   	  dataType:"text",
	   	  type:"post",
	   	  success:function(result){
			if(result=="exist"){
				flag = true;	 
			}
		  }	   	  
     });
	 
	 return flag;
}

function saveBookmark(name,author,lang,etype,eid){
	 var data={};
	 data["author"] = author;
	 data["name"] = name;
	 data["lang"] = lang;
	 data["etype"] = etype;
	 data["eid"] = eid;
	 data =  JSON.stringify(data);
	 data =("storeBookmark="+encodeURIComponent(data));
	 $.ajax({
	   	  url:views_servlet,
	   	  async:true,
	   	  data:data,
	   	  dataType:"text",
	   	  type:"post"		 
     });
}

/*show view content related*/
function showView(vid,vname,container){
	//cache part1
	////store current cache, only store 5 caches total(plus one empty one)
	if($(".vc_cache").find(".cache").length>=5){
       if($(".vc_cache").find(".cache:first").attr("vvid")!=vid){
    	   $(".vc_cache").find(".cache:first").remove();
       }else{
    	   $(".vc_cache").find(".cache").eq(1).remove();
       }
    }
 	var vvid = container.find(".relative").attr("vvid");
 	if(vvid){
 		var cacheOut = $("<div></div>");
 		cacheOut.attr("vid",vvid);
 		cacheOut.addClass("cache");
 		cacheOut.append(container.find(".relative"));
 		$(".vc_cache").append(cacheOut);
 	}
	
	container.html("");
	
	//cache part2
	var cacheIn = $(".vc_cache").find(".cache[vid="+vid+"]");
	if(cacheIn.length>0){
		container.append(cacheIn.find(".relative"));
		cacheIn.remove();
		return;
	}
	
	var vsectSpan = $('<span class="vsetc"><button class="vset ui-button ui-widget ui-state-default ui-corner-all ui-button-icons-only"  > <span class="icon-gear"></span><span class="icon-triangle-1-s"></span></button></span>');
	var menuDiv= null;
	if(displayLang=='en')
		menuDiv =$('<div class="dmenu" id="smenu"><ol><li class="mspacingLittle"></li> <li><a href="javascript:void(0)" class="view-layout">Set Layout</a></li></ol></div>');
	else if(displayLang=='zh')
		menuDiv =$('<div class="dmenu" id="smenu"><ol><li class="mspacingLittle"></li> <li><a href="javascript:void(0)" class="view-layout">设置布局</a></li></ol></div>');
	vsectSpan.append(menuDiv);
	if(vid>0){
		//first show vname
		var relativeDiv= $("<div class='relative'></div>");
		relativeDiv.attr("vvid",vid);
		var vnameSpan = $("<span class='vname'></span>").text(vname);
		relativeDiv.append(vnameSpan);
		var user = getUser();
		if(user!=null){
		   var hasAuthor = checkAuthority(vid)[0];
		   var gid = checkAuthority(vid)[1];
		   var isTrans = checkAuthority(vid)[2];
		   var transViewSrc;
		   var transViewLang;
		   if(isTrans==true){
			   transViewSrc = checkAuthority(vid)[3];
		   }
		   
		   //这边是否有权限还有用户当前选择的group角色有关
		   if($("#groupID").length==0){
			   hasAuthor = false;
		   }else{
			   if($("#groupID").val()!=gid){
				   hasAuthor = false;
			   }
		   }
		   
		   if(hasAuthor){
			   //这里还要判断是否是翻译生成的view，翻译生成的不再给他翻译机会
			   if(isTrans==false){
				   var e0 ;
			   	   if(displayLang=='en'){
			   		   e0 = $('<li><a href="javascript:void(0)" class="view-translate">Translate</a></li>');
			   	   }else if(displayLang=='zh'){
			   		   e0 = $('<li><a href="javascript:void(0)" class="view-translate">翻译</a></li>');
			   	   }
			   	   menuDiv.find("ol").prepend(e0) ;   
			   	   menuDiv.find("ol").prepend($('<li class="mspacingLittle"></li>')) ;  
			   }
			   
			   var el ;
			   if(isTrans==false){
				   if(displayLang=='en'){
					   el = $('<li><a href="javascript:void(0)" class="view-edit">Edit</a></li>');
				   }else if(displayLang=='zh'){
					   el = $('<li><a href="javascript:void(0)" class="view-edit">编辑</a></li>');
				   }
			   }else{
				   if(displayLang=='en'){
					   el = $('<li><a href="javascript:void(0)" class="transview-edit">Edit</a></li>');
				   }else if(displayLang=='zh'){
					   el = $('<li><a href="javascript:void(0)" class="transview-edit">编辑</a></li>');
				   }
				   el.find("a").attr("vid",transViewSrc);
			   }
			   menuDiv.find("ol").prepend(el) ;
		   }
		   else{
			   menuDiv.find(".mspacingLittle").remove();
		   }
		   relativeDiv.attr("gid",gid);
		   relativeDiv.append(vsectSpan.clone());
		}
		container.append(relativeDiv);
		var ul = $("<ul></ul>");
		relativeDiv.append(ul);
		//get the property list skeleton
		//ajax to get the property list and show the computing status
		var etype = parseInt(entityType);
		var eid = parseInt(entityID);
		getViewDatamember(vid,ul,etype,eid);
	}else{
		// forward and backward views
		//first show vname
		var relativeDiv= $("<div class='relative'></div>");
		relativeDiv.attr("vvid",vid);
		var vnameSpan = $("<span class='vname'></span>").text(vname);
		relativeDiv.append(vnameSpan);
		container.append(relativeDiv);
		
		var ul = $("<ul></ul>");
		relativeDiv.append(ul);
		//get the property list skeleton
		//ajax to get the property list and show the computing status
		var etype = parseInt(entityType);
		var eid = parseInt(entityID);
		getViewDatamember(vid,ul,etype,eid);
	}
}
/*fetch values 见 global.js*/

function storeLayout(user,vid,layout){
	 var data={};
	 data["user"] = user;
	 data["viewID"] = vid;
	 data["layout"] = layout;
	 data =  JSON.stringify(data);
	 data =("storelayout="+encodeURIComponent(data));
	 
	 $.ajax({
	   	  url:viewedit_servlet,
	   	  async:true,
	   	  data:data,
	   	  dataType:"text",
	   	  type:"post"
     });
}

/*tab cache*/
//read view cache to see if view has exist. If not ,return false;
function readViewCache(){
	var flag = false;
	
	var target;
	if($(".tab.selected>a").hasClass("myview")){
		target = $(".fv");
	}else if($(".tab.selected>a").hasClass("popview")){
		target = $(".av");
	}else if($(".tab.selected>a").hasClass("trashedview")){
		target = $(".tv");
	}
	
	if(target.length>0){
		flag = true;
		$(".view-content").html("");
		$(".view-content").append(target.children("div"));
		target.remove();
	}else{
		$(".view-content").html('<div class="vnav-box"><div class="vnav">'+
				'</div><div class="nv-box"></div></div><div class="vcontent" ></div>');
	}
	
	return flag;
}

function storeOldViewCache(element){
	var div = $("<div></div>");
	if($(element).find("a").hasClass("myview")){
		div.addClass("fv");
	}else if($(element).find("a").hasClass("popview")){
		div.addClass("av");
	}else if($(element).find("a").hasClass("trashedview")){
		div.addClass("tv");
	}

	div.append($(".view-content>div"));
	$(".tab-cache").append(div);
}



